package com.jb.java.tools;

import com.jb.web.bean.TextArea;
import com.jb.web.context.WebContext;
import com.jb.web.renderer.FormItemRenderer;

import com.jb.faces.component.UIComponent;


public class FileEditorRenderer extends FormItemRenderer {
    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        TextArea formField = (TextArea) itemToRender;
        StringBuffer html = new StringBuffer("");
        String value = formField.getValue();

        html.append(textAreaField(pCtx, formField, value));

        return html;
    }

    private String textAreaField(WebContext pCtx, TextArea formField, String value) {
        StringBuffer html = new StringBuffer("");

        if (formField.isEnabled()) {
            html.append("<TEXTAREA " + "name=\"" + formField.getFieldName() + "\"")
                .append(getEditScript(pCtx, formField)).append(addTagProperty("style", formField.getStyle(pCtx), null))
                .append(addTagProperty("class", formField.getStyleClass(), null)).append(">")
                .append((value == null) ? "" : value).append("</TEXTAREA>");
        } else {
            html.append("<DIV ").append(getEditScript(pCtx, formField)).append(addTagProperty("nowrap", "", null))
                .append(addTagProperty("style", formField.getStyle(pCtx), null)).append(">").append(value).append("&nbsp;</DIV>");
        }

        if (pCtx.isError()) {
            html.append(renderError(pCtx, formField));
        }

        return html.toString();
    }
}
